package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.ActivityEntity;
import com.afd.ozblip.entity.ActivityEntity;
import com.afd.ozblip.helper.ActivityVH;
import com.afd.ozblip.helper.CouponVH;
import com.afd.ozblip.image.LoadImage;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;

public class ActivityListAdapter extends BaseAdapter {

	public List<ActivityEntity> dataList;
	private LayoutInflater inflater;
	private Context context;
	private LoadImage loadImage;

	public ActivityListAdapter(Context paramContext) {
		context = paramContext;
		this.dataList = new ArrayList<ActivityEntity>();
		this.inflater = LayoutInflater.from(context);
		this.loadImage = new LoadImage(paramContext);
	}

	/**
	 * 获取列表条数
	 */
	@Override
	public int getCount() {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int paramInt) {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.get(paramInt);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		try {
			ActivityVH activityVH = null;
			if (paramView == null) {
				activityVH = new ActivityVH();
				paramView = this.inflater.inflate(R.layout.activity_list_item,
						null);

				activityVH.activityImageIv = (ImageView) paramView
						.findViewById(R.id.iv_activity_item_image);
				activityVH.activityTitleTv = (TextView) paramView
						.findViewById(R.id.tv_activity_item_title);
				activityVH.activityContentTv = (TextView) paramView
						.findViewById(R.id.tv_activity_item_content);
				activityVH.activityDateTv = (TextView) paramView
						.findViewById(R.id.tv_activity_item_date);
				paramView.setTag(activityVH);
			} else {
				activityVH = (ActivityVH) paramView.getTag();
			}

			ActivityEntity activity = this.dataList.get(position);
			if (activity != null) {
				// 活动图片
				String imageUrl = activity.getActivityImageUrl();
				if (!StringUtil.isNullOrEmpty(imageUrl)) {
					loadImage.addTask(Constants.WEB_PATH + imageUrl,
							activityVH.activityImageIv);
				} else {
					activityVH.activityImageIv
							.setImageDrawable(context.getResources()
									.getDrawable(R.drawable.default_pic));
				}
				// 活动标题
				String aTitle = activity.getActivityTitle();
				if (!StringUtil.isNullOrEmpty(aTitle)) {
					activityVH.activityTitleTv.setText(aTitle);
				}
				// 活动内容
				String aContent = activity.getActivityContent();
				if (!StringUtil.isNullOrEmpty(aContent)) {
					activityVH.activityContentTv.setText(aContent);
				}
				// 活动时间
				String aDate = activity.getActivityEndTime();
				if (!StringUtil.isNullOrEmpty(aDate)) {
					activityVH.activityDateTv.setText(aDate);
				}else{
					activityVH.activityDateTv.setText("暂无");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramView;
	}

	/**
	 * 添加多项
	 */
	public void addItems(List<ActivityEntity> commentList) {
		this.dataList.addAll(commentList);
	}

	/**
	 * 清除所有项
	 */
	public void removeAllItem() {
		this.dataList.clear();
	}
}
